package com.project.blog.service.impl;

import com.project.blog.common.util.UserUtils;
import com.project.blog.entity.Article;
import com.project.blog.entity.ArticleQuery;
import com.project.blog.entity.Tag;
import com.project.blog.entity.User;
import com.project.blog.repository.ArticleRepository;
import com.project.blog.repository.dao.ArticleDao;
import com.project.blog.repository.dao.TagDao;
import com.project.blog.service.ArticleService;
import com.project.blog.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleRepository articleRepository;


    @Resource
    private TagDao tagDao;
    @Resource
    private ArticleDao articleDao;


    @Override
    public PageResult<Article> getArticlesByTag(ArticleQuery query) {
        PageResult<Article> pageVo = new PageResult<>();

        List<Article> list = articleDao.getArticlesByTag(query, (query.getPageNum() - 1) * query.getPageSize(), query.getPageNum() * query.getPageSize());
        if (!list.isEmpty()) {
            for (Article article : list) {
                final List<Tag> tags = tagDao.getTagByArticleId(article.getId());
                article.setTags(tags);
            }
            pageVo.setList(list);
            pageVo.setTotal(list.get(0).getTotal());
        }
        return pageVo;
    }

    @Override
    public PageResult<Article> list(ArticleQuery query) {
        PageResult<Article> pageVo = new PageResult<>();
        pageVo.setPageNum(query.getPageNum());
        pageVo.setPageSize(query.getPageSize());
        List<Article> list = articleDao.list(query, (query.getPageNum() - 1) * query.getPageSize(), query.getPageNum() * query.getPageSize());
        if (!list.isEmpty()) {
            for (Article article : list) {
                final List<Tag> tags = tagDao.getTagByArticleId(article.getId());
                article.setTags(tags);
            }
            pageVo.setList(list);
            pageVo.setTotal(list.get(0).getTotal());
        }
        return pageVo;
    }

    @Override
    @Transactional
    public Article getArticleAndAddViews(Integer id) {
        int count = 1;
        Article article = articleRepository.getOne(id);
        article.setViewCounts(article.getViewCounts() + count);
        return article;
    }


    @Override
    public List<Article> listNewArticles() {
        ArticleQuery query = new ArticleQuery();
        query.setPageNum(1);
        query.setPageSize(10);
        query.setOrderCol("create_date");
        query.setOrderType("desc");
        List<Article> list = articleDao.list(query, (query.getPageNum() - 1) * query.getPageSize(), query.getPageNum() * query.getPageSize());
        return list;
    }

    @Override
    public Integer publishArticle(Article article) {

        if (null != article.getId()) {
            return this.updateArticle(article);
        } else {
            return this.saveArticle(article);
        }
    }

    @Override
    @Transactional
    public Integer saveArticle(Article article) {

        User currentUser = UserUtils.getCurrentUser();

        if (null != currentUser) {
            article.setAuthor(currentUser);
        }

        article.setCreateDate(new Date());
        article.setWeight(Article.Article_Common);

        return articleRepository.save(article).getId();
    }

    @Override
    @Transactional
    public Integer updateArticle(Article article) {
        Article oldArticle = articleRepository.getOne(article.getId());

        oldArticle.setTitle(article.getTitle());
        oldArticle.setSummary(article.getSummary());
        //  oldArticle.setBody(article.getBody());
        // oldArticle.setCategory(article.getCategory());
        //  oldArticle.setTags(article.getTags());

        return oldArticle.getId();
    }
}
